package com.fengxu.gamescript.util;

import java.lang.annotation.Annotation;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.AnnotatedElement;
import java.util.Arrays;

/**
 * 反射工具类
 */
public class ReflectUtils {

    /**
     * 判断obj是否标注了指定注解（支持组合注解）
     * @param obj 反射对象
     * @param targetAnnotation 目标注解
     * @return
     */
    public static  boolean isAnnotationPresent(Object obj, Class<? extends Annotation> targetAnnotation){
        AnnotatedElement element = obj.getClass();
        if(obj instanceof AnnotatedElement){
            element = (AnnotatedElement) obj;
            if(element.isAnnotationPresent(targetAnnotation)) return true;
        }
        if(obj instanceof Annotation){
            if(((Annotation) obj).annotationType() == targetAnnotation) return true;
            element = ((Annotation) obj).annotationType();
        }
        return Arrays.stream(element.getAnnotations())
                .anyMatch(annotation -> isAnnotationPresent(annotation.annotationType(), targetAnnotation));
    }

}
